package com.yellowpage.white.generator;

import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Table;

public class GeneratorStruts extends AberstractGenerator {

	public void generate() {
		String className = (String) this.properties.get("className");
		String dataPackage = (String) this.properties.get("dataPackage");
		String packageName = (String) this.properties.get("packageName");
		String templatePath = (String) this.properties.get("templatePath");
		String webFilePath = (String) this.properties.get("webFilePath");
		String webModulePath = (String) this.properties.get("webModulePath");

		String objectName = className.substring(0, 1).toLowerCase()
				+ className.substring(1);
		if (dataPackage == null || dataPackage.length() == 0) {
			dataPackage = packageName + ".data";
		}

		// 初始化生成器	
		this.setObject("package", packageName);
		this.setObject("className", className);

		this.setObject("objectName", objectName);
		this.setObject("dataPackage", dataPackage);
		this.setObject("daoPackage", packageName + ".dao");
		this.setObject("servicePackage", packageName + ".service");
		this.setObject("testPackage", packageName + ".test");
		this.setObject("actionPackage", packageName + ".action");
		this.setObject("webFilePath", webFilePath);

		this.initFields(dataPackage + "." + className);

		// 处理文件路径
		File tempFile = new File("");
		String realTemplatePath = tempFile.getAbsolutePath() + templatePath
				+ "\\";
		makePackageDir(tempFile.getAbsolutePath() + "\\src", packageName);
		
		// 处理WebModule的路径
		this.makeWebModuleDir(tempFile.getAbsolutePath() + webModulePath, webFilePath);

		
		// Action
		String targetActionPath = this.getPackageFilePath(tempFile
				.getAbsolutePath()
				+ "\\src\\", packageName + ".action");

		File actionTempFile = new File(realTemplatePath
				+ "WHITE_STRUTS_ACTION.tmp");
		File actionJavaFile = new File(targetActionPath + className
				+ "Action.java");
		this.generate(actionTempFile, actionJavaFile);

		// XML
		String targetXMLPath = this.getPackageFilePath(tempFile
				.getAbsolutePath()
				+ "\\src\\", packageName);

		File xmlTempFile = new File(realTemplatePath + "WHITE_STRUTS_CONFIG.tmp");
		File xmlFile = new File(targetXMLPath + className + "StrutsXML.txt");
		this.generate(xmlTempFile, xmlFile);


		// JSP
		String targetJSPPath = tempFile.getAbsolutePath() + "\\"
				+ webModulePath + "\\" + webFilePath + "\\";

		File jspQueryTempFile = new File(realTemplatePath
				+ "WHITE_STRUTS_JSPQUERY.tmp");
		File jspQueryFile = new File(targetJSPPath + className + "Query.jsp");
		this.generate(jspQueryTempFile, jspQueryFile);

		File jspAddTempFile = new File(realTemplatePath + "WHITE_STRUTS_JSPADD.tmp");
		File jspAddFile = new File(targetJSPPath + className + "Add.jsp");
		this.generate(jspAddTempFile, jspAddFile);

		File jspUpdateTempFile = new File(realTemplatePath
				+ "WHITE_STRUTS_JSPUPDATE.tmp");
		File jspUpdateFile = new File(targetJSPPath + className + "Update.jsp");
		this.generate(jspUpdateTempFile, jspUpdateFile);
	}

	public void makePackageDir(String srcPath, String packageName) {

		String[] packageArray = packageName.split("\\.");

		String currentPath = srcPath;
		for (int i = 0; i < packageArray.length; i++) {
			currentPath = currentPath + "\\" + packageArray[i];
			File tempFile = new File(currentPath);

			if (!tempFile.exists()) {
				System.out.println("创建目录：" + currentPath);
				tempFile.mkdir();
			}
		}

		File actionFile = new File(currentPath + "\\" + "action");
		if (!actionFile.exists()) {
			System.out.println("创建目录：" + actionFile.getAbsolutePath());

			actionFile.mkdir();
		}
	}
	
	public void initFields(String dataClass) {
		try{
		    Class dataObjectClass = Class.forName(dataClass);	    
		    
		    String className = dataObjectClass.getName();
		    className = className.substring(className.lastIndexOf('.') + 1);
		    
		    Table table = (Table)dataObjectClass.getAnnotation(Table.class);
		    
		    this.setObject("TableName", table.name());
		    
		    Method[] methods = dataObjectClass.getDeclaredMethods();
		    
		    for (int i=0; i<methods.length; i++) {
		        String methodName = methods[i].getName();
		        Column column = methods[i].getAnnotation(Column.class);				        
		        
		    	if(methodName.startsWith("get")){
		    		String fieldName = methodName.substring(3);
		    		String fieldType = methods[i].getGenericReturnType().toString();
		    		
		    		Map fieldMap = new HashMap();
					fieldMap.put("fieldName", fieldName);
					fieldMap.put("fieldInst", fieldName.substring(0,1).toLowerCase()+ fieldName.substring(1));
					fieldMap.put("fieldType", fieldType.substring(fieldType.lastIndexOf('.') + 1));
										
					if(column != null){
					    fieldMap.put("columnName", column.name());
					}
					
					
					if(fieldName.equals(className + "Id")){
						fieldList.add(0, fieldMap);
					}else{
					    fieldList.add(fieldMap);
					}
		    	}		    	
		    }
		    
		}catch(Exception e){
			e.printStackTrace();
		}	
		this.setObject("fieldList", fieldList);
		
	}

	public void makeWebModuleDir(String webModulePath, String webFilePath) {
		String[] webPathArray = webFilePath.split("/");

		String currentPath = webModulePath;
		for (int i = 0; i < webPathArray.length; i++) {
			currentPath = currentPath + "\\" + webPathArray[i];
			File tempFile = new File(currentPath);

			if (!tempFile.exists()) {
				System.out.println("创建目录：" + currentPath);
				tempFile.mkdir();
			}
		}
	}

}
